package com.zjeav.lingjiao.ui.personCenter;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dalimao.corelibrary.swipeback.SwipeBackActivity;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.request.ChangePwRequest;
import com.zjeav.lingjiao.base.request.ValidateCode;
import com.zjeav.lingjiao.base.response.ValidateCodeResponse;
import com.zjeav.lingjiao.base.utils.StringUtils;
import com.zjeav.lingjiao.base.view.TitleBar;
import com.zjeav.lingjiao.ui.register.RegisterView;

public class ModifyPasswordActivity extends SwipeBackActivity implements RegisterView, View.OnClickListener {

    private EditText username_ed;
    private EditText edit_sms;
    private EditText pw_ed;
    private EditText pw_confirm;
    private TextView tvsms;
    private TitleBar titleBar;
    private String titlename;
    private Button btnok;

    String code;
    private ChangePwRequest changePwRequest;
    private ModifyPasswordPresenter modifyPasswordPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_password_layout);

        username_ed = findViewById(R.id.username_ed);
        edit_sms = findViewById(R.id.edit_sms);
        pw_ed = findViewById(R.id.pw_ed);
        pw_confirm = findViewById(R.id.pw_confirm);
        tvsms = findViewById(R.id.tvsms);
        btnok = findViewById(R.id.btnok);
        titleBar = findViewById(R.id.titlebar);
        tvsms.setOnClickListener(this);
        btnok.setOnClickListener(this);
        titlename = getIntent().getExtras().getString("titlename", "修改密码");
        titleBar.setTitle(titlename);

        modifyPasswordPresenter = new ModifyPasswordPresenter(this);
    }

    @Override
    public void showCodeSuccess(ValidateCodeResponse response) {
        loadProgressDialog.dismiss();
        code = response.getData();
    }

    @Override
    public void showCodeFail() {
        loadProgressDialog.dismiss();
    }

    @Override
    public void registerSuccess(Result result) {
        loadProgressDialog.dismiss();
        finish();
        Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvsms:
                if (StringUtils.isNullOrEmpty(username_ed.getText().toString())) {
                    Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                } else {

                    //开启倒计时
                    timer.start();
                    tvsms.setClickable(false);
                    Toast.makeText(this, "短信已下发至+86 " + username_ed.getText().toString(), Toast.LENGTH_SHORT).show();

                    loadProgressDialog.show();
                    modifyPasswordPresenter.getCode(new ValidateCode(username_ed.getText().toString(),"3"));
                }
                break;
            case R.id.btnok:
                String phone = username_ed.getText().toString();
                String strsms = edit_sms.getText().toString();
                String strpassword = pw_ed.getText().toString();
                String stpw_confirm = pw_confirm.getText().toString();

                if (StringUtils.isNullOrEmpty(phone)) {
                    Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                } else if (StringUtils.isNullOrEmpty(strsms)) {
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                } else if (StringUtils.isNullOrEmpty(strpassword)) {
                    Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
                } else if (StringUtils.isNullOrEmpty(stpw_confirm)) {
                    Toast.makeText(this, "请输入确认密码", Toast.LENGTH_SHORT).show();
                } else if (!strpassword.equals(stpw_confirm)) {
                    Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    loadProgressDialog.show();
                    changePwRequest = new ChangePwRequest(phone, strsms, strpassword, stpw_confirm);
                    modifyPasswordPresenter.register(changePwRequest);
                }

                break;
            default:
                break;
        }
    }

    // 第一个参数表示总时间，第二个参数表示间隔时间。意思就是每隔一秒会回调一次方法onTick，然后60秒之后会回调onFinish方法
    final CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long arg0) {
            // 定期定期回调
            tvsms.setClickable(false);
            tvsms.setText((arg0 / 1000) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            // 结束后回到
            tvsms.setText("重新获取");
            tvsms.setClickable(true);
            tvsms.setBackground(getResources().getDrawable(R.drawable.buttonstyle));
        }
    };
}
